package listbyorder.access001_100.test16;

/**
 * 暴力穷举
 * @author code_yc
 * @version 1.0
 * @date 2020/5/28 9:05
 */
public class Solution1 {

    // 方法一：暴力穷举
    public int threeSumClosest(int[] nums, int target) {
        if (nums == null || nums.length < 3) return 0;
        int sum = Integer.MAX_VALUE;
        int ans = 0;
        for (int i = 0; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length - 1; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    if (Math.abs(nums[i] + nums[j] + nums[k] - target) < sum) {
                        ans = nums[i] + nums[j] + nums[k];
                        sum = Math.abs(ans - target);
                    }
                }
            }
        }
        return ans;
    }
}
